package com.ht.api.db.hlink.function.impl;

import javax.sql.DataSource;

import com.ht.api.db.Db;
import com.ht.api.db.Entity;
import com.ht.api.db.hlink.function.HlinkFunction;

import lombok.extern.slf4j.Slf4j;

/**
 * 获取获客链接客服关联表的main_id
 * @author asq
 * @createTime 2025年3月5日 21:46:52
 */
@Slf4j
public class GetMainIdByLinkRelationFunction implements HlinkFunction<String>{
	@Override
	public String format(DataSource ds, Object value, Entity entity) {
		String linkId = entity.getStr("link_id");
		String userid = entity.getStr("userid");
		
		// 找到关联表中的最新一条数据
		Entity wxCustomerLinkRelation = Db.createSqlParser("wx_customer_link_relation").where("link_id", linkId)
		.where("userid", userid).where("cancel_time", "is", null).findOne(ds);
		
		if (wxCustomerLinkRelation == null) {
			log.error("[GetMainIdByLinkRelationFunction]Hlink函数出错，根据linkId和userid没有找到关联数据，link_id:{}, userid:{}", 
					linkId, userid);
		}
		
		return wxCustomerLinkRelation.getMainId();
	}
}
